package cn.tedu.antcolony.passport.security;

import cn.tedu.antcolony.passport.pojo.vo.AdminLoginInfoVo;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 返回用户对象
 */
@Data
public class MySecurityUser implements Serializable, UserDetails {

    private final AdminLoginInfoVo adminLoginInfoVo;

    //用于存储权限的集合
    private List<SimpleGrantedAuthority> authorities;

    public MySecurityUser(AdminLoginInfoVo adminLoginInfoVo){
        this.adminLoginInfoVo=adminLoginInfoVo;
    }

    public AdminLoginInfoVo getAdminLoginInfoVo(){
        return adminLoginInfoVo;
    }

    public void setAuthorities(List<SimpleGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    /**
     * 返回用户所拥有的权限
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        String password = adminLoginInfoVo.getPassword();
        adminLoginInfoVo.setPassword(null);//擦除密码
        return password;
    }

    @Override
    public String getUsername() {
        return adminLoginInfoVo.getUsername();
    }

    /**
     * 当事人id
     * @return
     */
    public Long getUserId(){
        return adminLoginInfoVo.getId();
    }
    /**
     *账号没有过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账号没有锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码没有过期状态（true密码没有过期，false密码已经过期）
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账号是否启用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return adminLoginInfoVo.getEnable().equals(1);
    }
}
